#!/bin/bash -ex
#install build depend software
. /opt/hygon/common.sh
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if [ `id -u` -ne 0 ]; then
    echo "Must be run as root!"
    exit 1
fi
handle_error() {
    echo "======$0 FAIL,Please check ======"
    exit -1
}

readonly dcu_dir="${script_dir}/dcu"
mkdir -p "${dcu_dir}"

trap 'handle_error $?' ERR

if [ ! -f /sys/module/hycu_pci_fixup_header/version ];then
    ${script_dir}/../tools/sftp download /hygon-devkit/4.0/2025-01-01/csv/dcu/hycu_pci_fixup_header_0.4.4.bin ${dcu_dir}/hycu_pci_fixup_header_0.4.4.bin
    chmod +x ${dcu_dir}/hycu_pci_fixup_header_0.4.4.bin
    ${dcu_dir}/hycu_pci_fixup_header_0.4.4.bin
fi

mkdir -p /etc/qemu
cp -f /opt/hygon/csv/bridge.conf /etc/qemu/

os_release=$(eval get_os_release)
case $os_release in
    "anolis")
        setenforce  0 ||true
        systemctl disable firewalld.service ||true
        systemctl stop firewalld.service ||true
        yum remove -y podman buildah
        cp -f /opt/hygon/csv/kubernetes_anolis.repo /etc/yum.repos.d/kubernetes.repo

        yum install -y kubelet-1.27.0-00 kubectl-1.27.0-00 kubeadm-1.27.0-00 yum-utils device-mapper-persistent-data lvm2 python3
        [ ! -f /usr/bin/python ] && ln -s /usr/bin/python3 /usr/bin/python || true
        yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        yum install -y ninja-build.x86_64 glib2-devel.x86_64  pixman-devel.x86_64 libcap-ng-devel.x86_64  nasm iasl grub2-efi-x64-modules.noarch telnet python3-pip.noarch docker-ce docker-ce-cli containerd.io  libvirt  libvirt-client mysql
        systemctl enable --now libvirtd
        virsh net-start default ||true
        systemctl daemon-reload
        systemctl restart docker
        systemctl enable docker
        pip3 install qmp snowland-smx
    ;;
    "centos")
        yum install -y  glib2-devel.x86_64  pixman-devel.x86_64 libcap-ng-devel.x86_64  nasm iasl grub2-efi-x64-modules.noarch telnet python3-pip.noarch  yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        wget http://rpmfind.net/linux/centos/8-stream/BaseOS/x86_64/os/Packages/libseccomp-2.5.1-1.el8.x86_64.rpm
        rpm -e libseccomp-2.3.1-4.el7.x86_64 --nodeps
        rpm -ivh libseccomp-2.5.1-1.el8.x86_64.rpm --nodeps
        yum install -y devtoolset-8.x86_64 tigervnc.x86_64 docker-ce docker-ce-cli containerd.io
        systemctl daemon-reload
        systemctl restart docker
        systemctl enable docker
        pip3 install qmp
        wget https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-linux.zip
        unzip ninja-linux.zip
        cp ninja /usr/bin/
    ;;
    "ubuntu")
        ufw disable ||true
        apt-get -y install vim ssh git make  ninja-build gcc pkg-config libglib2.0-dev libpixman-1-dev libattr1-dev libcap-ng-dev uuid-dev nasm iasl software-properties-common dosfstools mtools python autoconf automake libtool autopoint tigervnc-viewer python3-pip apt-transport-https ca-certificates curl gnupg lsb-release zip mysql-client jq libvirt-daemon-system libvirt-clients bridge-utils
        pip3 install qmp snowland-smx
        systemctl enable --now libvirtd
        virsh net-start default ||true

        mkdir -p /etc/apt/keyrings
        curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
        echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

        apt-get update
        apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
        service docker start
        snap install yq --channel=v3/stable
        curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://dl.k8s.io/apt/doc/apt-key.gpg
        # Add Kubernetes apt repo
        echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list > /dev/null

        apt-get update
        echo y| apt-get install kubelet=1.27.0-00 kubectl=1.27.0-00 kubeadm=1.27.0-00
        apt-mark hold kubelet kubeadm kubectl
    ;;
    "kylin")
        setenforce  0 ||true
        systemctl disable firewalld.service ||true
        systemctl stop firewalld.service ||true
        cp -f /opt/hygon/csv/kubernetes_anolis.repo /etc/yum.repos.d/kubernetes.repo
        yum install -y elfutils-devel.x86_64 docker docker-compose skopeo.x86_64 kubelet-1.27.0-00 kubectl-1.27.0-00 kubeadm-1.27.0-00 socat python3-pip qemu.x86_64 qemu-img.x86_64 libvirt-daemon-driver-qemu.x86_64 libvirt-daemon-qemu.x86_64 mtools nasm acpica-tools edk2-ovmf libvirt-client.x86_64 libvirt-daemon-config-network.x86_64 docker ninja-build.x86_64 jq libvirt  libvirt-client bridge-utils
        pip3 install qmp snowland-smx
        systemctl enable --now libvirtd
        virsh net-start default ||true

        systemctl enable kubelet
        [ ! -f /usr/bin/qemu-system-x86_64 ] && ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-system-x86_64
        if [ -L "/opt/hygon/csv/OVMF_CODE.fd" ] || [ -f "/opt/hygon/csv/OVMF_CODE.fd" ]; then
            rm -f /opt/hygon/csv/OVMF_CODE.fd
        fi
        if [ -f /usr/share/edk2/ovmf-csv/OVMF_CODE.fd ];then
            ln -s /usr/share/edk2/ovmf-csv/OVMF_CODE.fd   /opt/hygon/csv/OVMF_CODE.fd || true
        else
            ln -s /usr/share/edk2/ovmf/OVMF_CODE.fd   /opt/hygon/csv/OVMF_CODE.fd || true 
        fi

    ;;
esac

